﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class AssessmentGroupDetails : Form
    {
        private AssessmentGroup _parent;
        public DataSets.Dictionary.AssessmentGroupDataTable AssessmentGroupData
        {
            get
            {
                return _parent.dictionary.AssessmentGroup;
            }
        }
        private Int32 _assessmentGroupId;

        public Int32 AssessmentGroupId
        {
            get { return _assessmentGroupId; }
            set { _assessmentGroupId = value; }
        }
        private DataSets.Dictionary.AssessmentGroupRow _curRow;

        public DataSets.Dictionary.AssessmentGroupRow CurrentRow
        {
            get { return _curRow; }
            set { _curRow = value; }
        }
        private Util.ActionType _openAction;

        public Util.ActionType OpenAction
        {
            get { return _openAction; }
            set { _openAction = value; }
        }


        private AssessmentGroupDetails()
        {
            InitializeComponent();
        }
        public AssessmentGroupDetails(Util.ActionType actType, Int32 assessmentGroupID, AssessmentGroup parent)
        {
            OpenAction = actType;
            AssessmentGroupId = assessmentGroupID;
            _parent = parent;
            InitializeComponent();
        }

        private void AssessmentGroupDetails_Load(object sender, EventArgs e)
        {
            switch (OpenAction)
            {
                case DIProject.Util.ActionType.Update:
                    InitUpdate();
                    break;
                case DIProject.Util.ActionType.Insert:
                    InitInsert();
                    break;
                default:
                    this.Close();
                    break;
            }

        }
        private void InitInsert()
        {
            btnDelete.Visible = false;
            btnInsertUpdate.Text = "Созранить";
        }
        private void InitUpdate()
        {
            btnInsertUpdate.Text = "Изменить";
            btnDelete.Visible = true;
            CurrentRow = this.AssessmentGroupData.FindById(AssessmentGroupId);
            txbName.Text = CurrentRow.Name;
            txbNote.Text = CurrentRow.Note;
        }
        private void AsseGrInsert()
        {
            DataSets.Dictionary.AssessmentGroupRow agr = this.AssessmentGroupData.NewAssessmentGroupRow();
            DataSets.DictionaryTableAdapters.AssessmentGroupTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.AssessmentGroupTableAdapter();
            agr.Name = txbName.Text;
            agr.Note = txbNote.Text;
            agr.MD = DateTime.Now;
            string MW = FirstForm.Login;
            if (string.IsNullOrEmpty(MW))
                MW = string.Empty;
            agr.MW = MW;
            this.AssessmentGroupData.Rows.Add(agr);
            adapter.Update(agr);// (this.AssessmentGroupData);
            this.AssessmentGroupData.AcceptChanges();
            
        }
        private void AsseGrUpdate()
        {
            DataSets.DictionaryTableAdapters.AssessmentGroupTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.AssessmentGroupTableAdapter();
            CurrentRow.Name = txbName.Text;
            CurrentRow.Note = txbNote.Text;
            CurrentRow.MD = DateTime.Now;
            string MW = FirstForm.Login;
            if (string.IsNullOrEmpty(MW))
                MW = string.Empty;
            CurrentRow.MW = MW;
            adapter.Update(CurrentRow);// (this.AssessmentGroupData);
            this.AssessmentGroupData.AcceptChanges();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInsertUpdate_Click(object sender, EventArgs e)
        {
            switch (OpenAction)
            {
                case DIProject.Util.ActionType.Insert:
                    this.AsseGrInsert();
                    break;
                case DIProject.Util.ActionType.Update:
                    this.AsseGrUpdate();
                    break;
                default:
                    break;
            }
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Вы действительно хотите удалить запись " + CurrentRow.Name + "?", "Удаление записи", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DataSets.DictionaryTableAdapters.AssessmentGroupTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.AssessmentGroupTableAdapter();
                CurrentRow.Delete();
                adapter.Update(this.AssessmentGroupData);
                this.AssessmentGroupData.AcceptChanges();
            }
            this.Close();
        }
    }
}